//
//  JWShopMeShopTimeVC.m
//  jinwang
//
//  Created by iOS on 2018/4/18.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopMeShopTimeVC.h"

@interface JWShopMeShopTimeVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *gotoworkLabel;
@property (weak, nonatomic) IBOutlet UIButton *gotoworkBtn;
@property (weak, nonatomic) IBOutlet UILabel *offworkLabel;
@property (weak, nonatomic) IBOutlet UIButton *offworkBtn;


@property (nonatomic, strong) UIView *bgPickView;
@property (nonatomic, strong) UIPickerView *packView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, copy) NSString *time;
@property (nonatomic, copy) NSString *gotoworkTime;
@property (nonatomic, copy) NSString *offworkTime;

@property (nonatomic, copy) NSString *gotoworkShiTime;
@property (nonatomic, copy) NSString *gotoworkFenTime;

@property (nonatomic, copy) NSString *offworkShiTime;
@property (nonatomic, copy) NSString *offworkFenTime;

@end

@implementation JWShopMeShopTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavi];
    [self createUI];

}

-(void)createNavi{
    self.title = @"上下班时间";

}


-(void)createUI{
    
    
    self.bgPickView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 230)];
    self.bgPickView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.bgPickView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(10, 10, 50, 30);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgPickView addSubview:cancelBtn];
    
    
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(self.bgPickView.frame.size.width - 10 - 50 , 10, 50, 30);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(pickSureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgPickView addSubview:sureBtn];
    
    
    
    self.packView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, kScreenWidth, 180)];
    self.packView.delegate = self;
    self.packView.dataSource = self;
    self.packView.backgroundColor = [UIColor whiteColor];
    
    [self.bgPickView addSubview:self.packView];
    
    
}



// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 4;
}

// 返回每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 24;
    }
    else if (component == 1){
        return 1;
    }
    else if (component == 2){
        return 60;
    }
    else{
        return 1;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        return [NSString stringWithFormat:@"%02ld",row];
    }
    else if (component == 1){
        return @"时";
    }
    else if(component == 2){
        return [NSString stringWithFormat:@"%02ld",row];

    }
    else{
        return @"分";
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if ([self.time isEqualToString:@"gotowork"]) {
        
        if (component == 0) {
            self.gotoworkShiTime = [NSString stringWithFormat:@"%02ld",row];
        }
        if (component == 2) {
             self.gotoworkFenTime = [NSString stringWithFormat:@"%02ld",row];
        }
        
    }
    else{
        if (component == 0) {
            self.offworkShiTime = [NSString stringWithFormat:@"%02ld",row];
        }
        if (component == 2) {
            self.offworkFenTime = [NSString stringWithFormat:@"%02ld",row];
        }
        
    }
    
}

//返回指定列，行的高度，就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 44.0f;
    
}


-(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSString *currentTime = [currentTimeString substringWithRange:NSMakeRange(11, 5)];
    return currentTime;
    
}



- (IBAction)gotoworkBtnAction:(UIButton *)sender {
    self.time = @"gotowork";
    
    NSString *time = [self getCurrentTimes];
    
    NSInteger shi = [[time substringWithRange:NSMakeRange(0, 2)] intValue];
    NSInteger fen = [[time substringWithRange:NSMakeRange(3, 2)] intValue];
    
    self.gotoworkShiTime = [NSString stringWithFormat:@"%ld",shi];
    self.gotoworkFenTime = [NSString stringWithFormat:@"%ld",fen];

    [self.packView selectRow:shi inComponent:0 animated:YES];
    [self.packView selectRow:fen inComponent:2 animated:YES];
    
    [UIView animateWithDuration:0.7 animations:^{
        self.bgPickView.frame = CGRectMake(0, kScreenHeight - 230, kScreenWidth, 230);
    }];
}

- (IBAction)offworkBtnAction:(UIButton *)sender {
    
    self.time = @"offwork";
    
    NSString *time = [self getCurrentTimes];
    
    NSInteger shi = [[time substringWithRange:NSMakeRange(0, 2)] intValue];
    NSInteger fen = [[time substringWithRange:NSMakeRange(3, 2)] intValue];
    
    self.offworkShiTime = [NSString stringWithFormat:@"%ld",shi];
    self.offworkFenTime = [NSString stringWithFormat:@"%ld",fen];
    
    [self.packView selectRow:shi inComponent:0 animated:YES];
    [self.packView selectRow:fen inComponent:2 animated:YES];
    
    [UIView animateWithDuration:0.7 animations:^{
        self.bgPickView.frame = CGRectMake(0, kScreenHeight - 230, kScreenWidth, 230);

    }];
}


- (IBAction)sureBtnAction:(UIButton *)sender {
    
}


-(void)cancelBtnAction:(UIButton*)btn{
    
    [UIView animateWithDuration:0.7 animations:^{
        self.bgPickView.frame = CGRectMake(0,kScreenHeight, kScreenWidth, 230);
        
    }];
}

-(void)pickSureBtnAction:(UIButton*)btn{
    
    if ([self.time isEqualToString:@"gotowork"]) {
        self.gotoworkLabel.text = [NSString stringWithFormat:@"%@:%@",self.gotoworkShiTime,self.gotoworkFenTime];
        
    }
    else{
        self.offworkLabel.text = [NSString stringWithFormat:@"%@:%@",self.offworkShiTime,self.offworkFenTime];
    }
    
    [UIView animateWithDuration:0.7 animations:^{
        self.bgPickView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 230);
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
